package cetcbigdata.da.service.guangdong.to_do_list;

import cetcbigdata.da.base.Base;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class GuangDong extends Base {

    private static String baseRegion = "广东省";

    private static String baseModule = "服务事项-即办件事项清单";

    @Override
    public JSONObject getDataDemo() {
        try {
            return processResult("xxx", "xxx", "xxx", "xxx", new JSONObject(), "ss", "ss");
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {
        try {
            for(JSONObject r : getRegion()){
                for(JSONObject m : getModule()){
                    this.parseListPage(recordSender, fieldsList, r, m);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private JSONObject parseListPage(RecordSender recordSender, List<Object> fieldsList, JSONObject regionItem, JSONObject moduleItem){
        //获取各区编码和类型编码
        String regCode = regionItem.getString("code");
        String code =  moduleItem.getString("code");

        //获取各区名称和类型名称
        String region = baseRegion + "-" + regionItem.getString("name");
        String module = baseModule + "-" + moduleItem.getString("name");
        try{
            // 初始化总页码数
            int pages = 0;
            // 当前页
            int currentPage = 1;
            // 如果当前也不等于总页码数，则一直循环 ，实现翻页
            while (true){
                String listUrl =
                        "https://www.gdzwfw.gov.cn/portal/api/v2/item/getPromiseItem?qdType=12&areaCode=" + regCode + "&islocallevel=0&ONLINECHECK=&deptCode=&taskType=" + code + "&pageNo="
                                + currentPage +"&pageSize=10";
                JSONObject headers = new JSONObject();
                headers.put("Referer", "https://www.gdzwfw.gov.cn/portal/v2/affairs-public-second-list?region=440000&type=12");
                headers.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36");
                JSONObject parmas = new JSONObject();
                String listResponse =  getReq(listUrl, headers, parmas);
                JSONObject responseObj = JSONObject.parseObject(listResponse);
                if(responseObj.getInteger("code").equals(0)){
                    JSONArray listTemp = responseObj.getJSONArray("list");
                    JSONObject listPageObj = new JSONObject();
                    for(Object o : listTemp){
                        JSONObject item = (JSONObject) o;
                        String taskCode = item.getString("serviceCode");
                        if(listPageObj.containsKey(taskCode)){
                            listPageObj.getJSONArray(taskCode).add(item);
                        }else {
                            listPageObj.put(taskCode, new JSONArray());
                            listPageObj.getJSONArray(taskCode).add(item);
                        }
                    }

                    // 每页每项遍历
                    for(String key : listPageObj.keySet() ){
                        JSONObject contentParent =new JSONObject();
                        String title = null;
                        String detailUrl = null;
                        String contentResponse = null;
                        for(Object o : listPageObj.getJSONArray(key)){
                            JSONObject item = (JSONObject) o;
                            String itemCode = item.getString("ywCode");
                            if (null == itemCode){
                                detailUrl = String.format("https://www.gdzwfw.gov.cn/portal/v2/guide/%s",
                                        item.getString("serviceCode"));
                                title = item.getString("serviceName");
                                contentParent.put("名称", title);
                                contentParent.put("网址", detailUrl);
                                processResult(listUrl, listResponse,detailUrl,contentResponse, contentParent,
                                        region,
                                        module
                                );
                            }
                            else {
                                detailUrl = String.format("https://www.gdzwfw.gov.cn/portal/v2/guide/%s",
                                        item.getString("ywCode"));
                                title = item.getString("ywName");
                                contentParent.put("名称", title);
                                contentParent.put("网址", detailUrl);
                                processResult(listUrl, listResponse, detailUrl, contentResponse, contentParent,
                                        region,
                                        module
                                );
                            }
                        }
                    }
                }
                // 更新当前页
                currentPage = responseObj.getInteger("pageNo") + 1;
                // 更新总页数
                int total = responseObj.getInteger("total_rows");
                int size = responseObj.getInteger("pageSize");
                int prePages = total/size;
                pages = total%size == 0 ? prePages : prePages + 1;
                if (currentPage > pages){
                    break;
                }
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return new JSONObject();
    }

    //获取市名和各市编码
    private static JSONArray getCityList(){
        String city = " [\n" +
                "{\"name\":\"广州市\",\"region\":\"440100\"},    \n" +
                "{\"name\":\"深圳市\",\"region\":\"440300\"}, \n" +
                "{\"name\":\"珠海市\",\"region\":\"440400\"},\n" +
                "{\"name\":\"汕头市\",\"region\":\"440500\"},\n" +
                "{\"name\":\"佛山市\",\"region\":\"440600\"},\n" +
                "{\"name\":\"韶关市\",\"region\":\"440200\"},\n" +
                "{\"name\":\"河源市\",\"region\":\"441600\"},\n" +
                "{\"name\":\"梅州市\",\"region\":\"441400\"},\n" +
                "{\"name\":\"惠州市\",\"region\":\"441300\"},\n" +
                "{\"name\":\"汕尾市\",\"region\":\"441500\"},\n" +
                "{\"name\":\"东莞市\",\"region\":\"441900\"},\n" +
                "{\"name\":\"中山市\",\"region\":\"442000\"},\n" +
                "{\"name\":\"江门市\",\"region\":\"440700\"},\n" +
                "{\"name\":\"阳江市\",\"region\":\"441700\"},\n" +
                "{\"name\":\"湛江市\",\"region\":\"440800\"},\n" +
                "{\"name\":\"茂名市\",\"region\":\"440900\"},\n" +
                "{\"name\":\"肇庆市\",\"region\":\"441200\"},\n" +
                "{\"name\":\"揭阳市\",\"region\":\"445200\"},\n" +
                "{\"name\":\"清远市\",\"region\":\"441800\"},\n" +
                "{\"name\":\"潮州市\",\"region\":\"445100\"},\n" +
                "{\"name\":\"云浮市\",\"region\":\"445300\"},\n" +
//                "{\"name\":\"横琴粤澳深度合作区\",\"region\":\"440405\"}\n" +
                " ]";
        return JSONArray.parseArray(city);
    }

    private static List<JSONObject> getRegion() {
        //获取各市编码
        JSONArray cityList = getCityList();
        List<JSONObject> out = new ArrayList<>();
        for (Object o : cityList){
            JSONObject oo = (JSONObject) o;
            String code = oo.getString("region");

            //请求获取各市包含的区的名称和编码
            String url = "https://www.gdzwfw.gov.cn/portal/custom-config/gdbsNav/getDetail";
            JSONObject header = new JSONObject();
            header.put("Referer", "https://apps.gdzwfw.gov.cn/");
            header.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0");
            JSONObject params = new JSONObject();
            params.put("regCode", code);

            //获取各个市的所有区的源数据
            String response = postFormReq(url, header, params);
            JSONObject responseObj = JSONObject.parseObject(response);
            if (responseObj.getString("statusCode").equals("200")){
                JSONArray dataList = responseObj.getJSONObject("data").getJSONObject("data").getJSONArray("country");

                //判断各个市是否包含区
                if (dataList.size() != 0){
                    for (Object d : dataList){
                        JSONObject dd = (JSONObject) d;
                        JSONObject outItem = new JSONObject();
                        String name = oo.getString("name") + "-" + dd.getString("ORGSNAME");
                        outItem.put("name", name);
                        outItem.put("code", dd.getString("ORGAREACODE"));
                        out.add(outItem);
                    }
                }else {
                    JSONObject outItem = new JSONObject();
                    String name = oo.getString("name");
                    outItem.put("name", name);
                    outItem.put("code", oo.getString("region"));
                    out.add(outItem);
                }
            }
        }
        return out;
    }

    private static List<JSONObject> getModule(){
        //获取类型名称和编码
        String data = "{\"THEMES\":[{\n" +
                "                    TASK_TYPE: \"01\",\n" +
                "                    text: \"行政许可\",\n" +
                "                    key: \"XK\",\n" +
                "                    num: \"\"\n" +
                "                }, {\n" +
                "                    TASK_TYPE: \"11\",\n" +
                "                    text: \"公共服务\",\n" +
                "                    key: \"FW\",\n" +
                "                    num: \"\"\n" +
                "                }, {\n" +
                "                    TASK_TYPE: \"02\",\n" +
                "                    text: \"行政处罚\",\n" +
                "                    key: \"CF\",\n" +
                "                    num: \"\"\n" +
                "                }, {\n" +
                "                    TASK_TYPE: \"03\",\n" +
                "                    text: \"行政强制\",\n" +
                "                    key: \"QZ\",\n" +
                "                    num: \"\"\n" +
                "                }, {\n" +
                "                    TASK_TYPE: \"04\",\n" +
                "                    text: \"行政征收\",\n" +
                "                    key: \"ZS\",\n" +
                "                    num: \"\"\n" +
                "                }, {\n" +
                "                    TASK_TYPE: \"05\",\n" +
                "                    text: \"行政给付\",\n" +
                "                    key: \"GF\",\n" +
                "                    num: \"\"\n" +
                "                }, {\n" +
                "                    TASK_TYPE: \"06\",\n" +
                "                    text: \"行政检查\",\n" +
                "                    key: \"JC\",\n" +
                "                    num: \"\"\n" +
                "                }, {\n" +
                "                    TASK_TYPE: \"07\",\n" +
                "                    text: \"行政确认\",\n" +
                "                    key: \"QR\",\n" +
                "                    num: \"\"\n" +
                "                }, {\n" +
                "                    TASK_TYPE: \"08\",\n" +
                "                    text: \"行政奖励\",\n" +
                "                    key: \"JL\",\n" +
                "                    num: \"\"\n" +
                "                }, {\n" +
                "                    TASK_TYPE: \"09\",\n" +
                "                    text: \"行政裁决\",\n" +
                "                    key: \"CJ\",\n" +
                "                    num: \"\"\n" +
                "                }, {\n" +
                "                    TASK_TYPE: \"10\",\n" +
                "                    text: \"其他\",\n" +
                "                    key: \"QT\",\n" +
                "                    num: \"\"\n" +
                "                }]}";
        JSONArray validData = JSONObject.parseObject(data).getJSONArray("THEMES");
        List<JSONObject> out = new ArrayList<>();
        for(Object o : validData){
            JSONObject oItem = (JSONObject) o;
            JSONObject outItem = new JSONObject();
            outItem.put("code", oItem.getString("TASK_TYPE"));
            outItem.put("name", oItem.getString("text"));
            out.add(outItem);
        }
        return out;
    }

    public static void main(String[] args) {
        GuangDong spider = new GuangDong();
        spider.getData(null, new ArrayList<>());
    }
}
